package vy.org.app.page.address;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.vy.dal.dao.VyAddressDO;

import vy.org.app.common.ActionCtx;
import vy.org.app.common.AddressTypeEnum;

public class AddressHelper {

	public static SimpleDateFormat _DATE_FMT = new SimpleDateFormat("MM/dd/yyyy");
	
	public static AddressForm getAddress(AddressTypeEnum type, List<VyAddressDO> lst) {
		AddressForm addr = null;
		for(VyAddressDO dto : lst){
			if(type.getName().equals(dto.getAddressType())) {
				addr = new AddressForm();
				addr.setAddressId(dto.getAddressId().toString());
				addr.setAddressType(dto.getAddressType());
				addr.setNamePrefix(dto.getNamePrefix());
				addr.setCoName(dto.getCoName());
				addr.setCity(dto.getCity());
				addr.setCountry(dto.getCountry());
				addr.setDeleted(dto.getIsDeleted());
				addr.setDistrictOrCounty(dto.getDistrictOrCounty());
				addr.setPostBox(dto.getPostBox());
				if(dto.getEffectiveDate() != null)
					addr.setEffectiveDate(_DATE_FMT.format(dto.getEffectiveDate()));
				addr.setLastUpdatedBy(dto.getLastUpdatedBy());
				if(dto.getLastUpdatedOn() != null)
					addr.setLastUpdatedOn(_DATE_FMT.format(dto.getLastUpdatedOn()));
				addr.setPhone1(dto.getPhone1());
				addr.setPhone2(dto.getPhone2());
				addr.setFax(dto.getFax());
				addr.setRefId(dto.getRefId());
				addr.setStateOrRegion(dto.getStateOrRegion());
				addr.setStreet1(dto.getStreet1());
				addr.setStreet2(dto.getStreet2());
				addr.setZip(dto.getZip());
				addr.setStreetHindi1(dto.getStreetHindi1());
				addr.setStreetHindi2(dto.getStreetHindi2());
				addr.setPrintLanguage(dto.getPrintLanguage());
				break;
			}
		}
		return addr;
	}
	
	public static AddressForm getAddress(VyAddressDO dto) {
		AddressForm addr = null;
		addr = new AddressForm();
		addr.setAddressId(dto.getAddressId().toString());
		addr.setAddressType(dto.getAddressType());
		addr.setNamePrefix(dto.getNamePrefix());
		addr.setCoName(dto.getCoName());
		addr.setCity(dto.getCity());
		addr.setCountry(dto.getCountry());
		addr.setDeleted(dto.getIsDeleted());
		addr.setDistrictOrCounty(dto.getDistrictOrCounty());
		addr.setPostBox(dto.getPostBox());
		if(dto.getEffectiveDate() != null)
			addr.setEffectiveDate(_DATE_FMT.format(dto.getEffectiveDate()));
		addr.setLastUpdatedBy(dto.getLastUpdatedBy());
		if(dto.getLastUpdatedOn() != null)
			addr.setLastUpdatedOn(_DATE_FMT.format(dto.getLastUpdatedOn()));
		addr.setPhone1(dto.getPhone1());
		addr.setPhone2(dto.getPhone2());
		addr.setFax(dto.getFax());
		addr.setRefId(dto.getRefId());
		addr.setStateOrRegion(dto.getStateOrRegion());
		addr.setStreet1(dto.getStreet1());
		addr.setStreet2(dto.getStreet2());
		addr.setZip(dto.getZip());
		addr.setStreetHindi1(dto.getStreetHindi1());
		addr.setStreetHindi2(dto.getStreetHindi2());
		addr.setPrintLanguage(dto.getPrintLanguage());
		return addr;
	}
	
	public static VyAddressDO getAddressDO(AddressForm addr) {
		VyAddressDO dto = new VyAddressDO();
		
//		if(type.getName().equals(dto.getAddressType())) {
		if(addr.getAddressId() != null && !"".equals(addr.getAddressId()))
			dto.setAddressId(Integer.parseInt(addr.getAddressId()));
			dto.setAddressType(addr.getAddressType());
			dto.setNamePrefix(addr.getNamePrefix());
			dto.setCoName(addr.getCoName());
			dto.setCity(addr.getCity());
			dto.setCountry(addr.getCountry());
			dto.setIsDeleted(addr.isDeleted());
			dto.setDistrictOrCounty(addr.getDistrictOrCounty());
			dto.setPostBox(addr.getPostBox());
//			if(addr.getEffectiveDate() != null)
//				dto.setEffectiveDate(_DATE_FMT.format(addr.getEffectiveDate()));
			dto.setLastUpdatedBy(ActionCtx.ctx().getUser().getId());
			dto.setLastUpdatedOn(new Date());
			dto.setPhone1(addr.getPhone1());
			dto.setPhone2(addr.getPhone2());
			dto.setFax(addr.getFax());
			dto.setRefId(addr.getRefId());
			dto.setStateOrRegion(addr.getStateOrRegion());
			dto.setStreet1(addr.getStreet1());
			dto.setStreet2(addr.getStreet2());
			dto.setZip(addr.getZip());
			dto.setStreetHindi1(addr.getStreetHindi1());
			dto.setStreetHindi2(addr.getStreetHindi2());
			dto.setPrintLanguage(addr.getPrintLanguage());
//		}
		return dto;
	}

}
